<template>
  <div class="container">
    <el-form :inline="true">
      <el-form-item>
        <el-input
          v-model="pageGrid.orderNo"
          placeholder="内部订单号"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="pageGrid.account" placeholder="工号"></el-input>
      </el-form-item>
      <el-form-item>
        <div class="d-flex">
          <el-button type="primary" @click="getData">搜索</el-button>
          <el-button @click="clearSearch">重置</el-button>
          <el-upload
            v-if="isAdmin"
            class="upload-demo"
            :action="exportUrl"
            :data="uploadData"
            :limit="1"
            :file-list="fileList"
            :on-success="successUpload"
            :show-file-list="false"
          >
            <el-button size="small" type="primary">导入文件</el-button>
          </el-upload>
        </div>
      </el-form-item>
    </el-form>
    <el-table v-loading="isLoading" :data="tableData" border class="table">
      <el-table-column
        prop="orderNo"
        label="内部订单号"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="seque"
        label="序号"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="workmanship"
        label="工序"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="specs"
        label="规格"
        align="center"
      ></el-table-column>
      <el-table-column label="分配" align="center">
        <el-table-column
          prop="number1"
          label="工号"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="amount1"
          label="数量"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="number2"
          label="工号"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="amount2"
          label="数量"
          align="center"
        ></el-table-column>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="text" @click="deleteWorkDone(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageGrid.pageNo"
        :page-sizes="pageSizeArr"
        :page-size="pageGrid.pageSize"
        layout="total, sizes, prev, pager, next"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import api from "../../../api";
import commonInfo from "@/utils/common";
import host from "@/utils/host";

export default {
  name: "workDone",
  data() {
    return {
      pageSizeArr: commonInfo.pageSizeArr,
      isLoading: false,
      tableData: [],
      total: 0,
      pageGrid: {
        pageNo: 1,
        pageSize: 10,
        orderNo: null,
        account: null,
      },
      exportUrl: host.host + "file/open/fileUpload",
      uploadData: {
        type: "workDone",
      },
      fileList: [],
      isAdmin: false,
    };
  },
  created() {
    this.getData();
    let user = sessionStorage.getItem("adminUserInfo");
    if (user) this.isAdmin = JSON.parse(user).role === "管理员";
  },
  methods: {
    getData() {
      this.isLoading = true;
      api
        .getWorkDonePage(this.pageGrid)
        .then((res) => {
          if (res.code === 200) {
            this.tableData = res.totalElement;
            this.total = res.total;
            this.isLoading = false;
          }
        })
        .catch((this.isLoading = false));
    },
    successUpload(response, file, fileList) {
      this.fileList = [];
      api.importWorkDone({ path: response.data }).then((res) => {
        this.getData();
        this.$message.success("导入成功");
      });
    },
    deleteWorkDone(row) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          api.deleteWorkDone({ id: row.id }).then((res) => {
            if (res.code == 200) {
              this.$message.success("删除成功");
              this.pageGrid.pageNo = 1;
              this.getData();
            } else {
              this.$message.error(res.msg);
            }
          });
        })
        .catch(() => {});
    },
    clearSearch() {
      this.pageGrid = {
        pageNo: 1,
        pageSize: 10,
        parameter: {
          orderNo: null,
          account: null,
        },
      };
      this.getData();
    },
    //分页
    handleCurrentChange(val) {
      this.pageGrid.pageNo = val;
      this.getData();
    },
    //分页
    handleSizeChange(val) {
      this.pageGrid.pageSize = val;
      this.getData();
    },
  },
};
</script>

<style scoped lang="less">
.upload-demo {
  height: 32px;
  display: inline-block;
  margin-left: 10px;
}
</style>
